/**
 * 
 */
package com.mc.yingshi;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 客流统计
 * @author w.weizhen
 *
 */
public class Passengerflow {
	private static String accessToken="at.9dqd3zdia5jp0s2p8b0je3846q0axaau-7hzf8jmtd3-1wwlzno-sulojari4";
    private static String daily ="https://open.ys7.com/api/lapp/passengerflow/daily";//查询设备某一天的统计客流数据
    private static String switchStatus ="https://open.ys7.com/api/lapp/passengerflow/switch/status";//获取客流统计开关状态
    private static String switchSet ="https://open.ys7.com/api/lapp/passengerflow/switch/set";//设置客流统计开关
    private static String hourly ="https://open.ys7.com/api/lapp/passengerflow/hourly";//查询设备某一天每小时的客流数据
    private static String configGet ="https://open.ys7.com/api/lapp/passengerflow/config/get";//获取客流统计配置信息
	public static void main(String[] args) {
		//switchStatus();//获取客流统计开关状态
		switchSet();//设置客流统计开关
		//daily();//查询设备某一天的统计客流数据
		//hourly();//查询设备某一天每小时的客流数据
		//getConfig();//获取客流统计配置信息
    }
    //获取客流统计开关状态
    private static void switchStatus()
    {
    	OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)//授权过程获取的access_token
            .add("deviceSerial", "G07098865")//直播源，例如427734222，均采用英文符号，限制50个
            .build();
        Request request = new Request.Builder()
            .url(switchStatus)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //设置客流统计开关
    private static void switchSet()
    {
    	OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)//授权过程获取的access_token
            .add("deviceSerial", "G07098865")//直播源，例如427734222，均采用英文符号，限制50个
            .add("channelNo", "")//通道号，不传表示设备本身
            .add("enable", "1")//状态：0-关闭，1-开启
            .build();
        Request request = new Request.Builder()
            .url(switchSet)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //查询设备某一天的统计客流数据
    private static void daily()
    {
        // 将日期字符串,转为时间戳[毫秒]
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse("2022-05-30 00:00:00", dtf);
        Long epochMilli = LocalDateTime.from(ldt).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        String date = epochMilli.toString();
        
    	OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)//授权过程获取的access_token
            .add("deviceSerial", "G07098865")//直播源，例如427734222，均采用英文符号，限制50个
            .add("channelNo", "1")//通道号
            .add("date", date)//时间戳日期，精确至毫秒，默认为今天，date参数只能是0时0点0分0秒（如1561046400000可以，1561050000000不行
            .build();
        Request request = new Request.Builder()
            .url(daily)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //查询设备某一天每小时的客流数据
    private static void hourly()
    {
     // 将日期字符串,转为时间戳[毫秒]
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse("2022-05-29 00:00:00", dtf);
        Long epochMilli = LocalDateTime.from(ldt).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        String date = epochMilli.toString();
        
    	OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)//授权过程获取的access_token
            .add("deviceSerial", "G07098865")//直播源，例如427734222，均采用英文符号，限制50个
            .add("channelNo", "1")//通道号
            .add("date", date)//时间戳日期，精确至毫秒，默认为今天，date参数只能是0时0点0分0秒（如1561046400000可以，1561050000000不行
            .build();
        Request request = new Request.Builder()
            .url(hourly)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获取客流统计配置信息
    private static void getConfig()
    {
    	OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)//授权过程获取的access_token
            .add("deviceSerial", "G07098865")//直播源，例如427734222，均采用英文符号，限制50个
            .build();
        Request request = new Request.Builder()
            .url(configGet)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
